# -*- coding: utf-8 -*-
__author__ = 'alex'
import re
from pyparsing_py2 import *
from commonRoutine import *

inf_name_re = u"[A-Za-zА-Яа-яЁё_ -]*"
name_re = Regex(inf_name_re)
line_re = Suppress(u"|") + name_re + Suppress(u"=") + restOfLine
atributeSet = OneOrMore(Group(line_re))

#TODO: NOTE, THAT IT WILL MATCH ending}} IF NOT AT THE SEPARATE LINE
#infoboxBodyGetter = re.compile(u"\{\{\s*(.*)\n((^\s*\|.*?\n)*)", re.MULTILINE | re.UNICODE)
#infoboxBodyGetter = re.compile(u"\{\{\s*(.*)\n((^\s*\|.*?=.*?\n)*)", re.MULTILINE | re.UNICODE)
infoboxBodyGetter = re.compile(u"\{\{\s*([A-Za-zА-Яа-яЁё_ -]*).*\n((^\s*\|.*?=.*?\n)*)", re.MULTILINE | re.UNICODE)


class WikiAttribParser(object):
    def __init__(self, infPageDescription):
        self._infPageDescription = infPageDescription
        self._attrib = {}

    def checkForInteresting(self, page_id):
        return page_id in self._infPageDescription

    def getNewAttrib(self, page):
        infobox = self.getAttrib(page)
        if infobox is None:
            return None

        inf_id = infobox[0]
        attribOnThisPage = infobox[1]
        newAttrib = []

        if inf_id not in self._attrib:
            self._attrib[inf_id] = set()

        #TODO: delete from here
        """
        if inf_id == u"8887674":
            print "\npage_id", page._id

        if page._id == u"324":
            print "DUBLICATE DEGUB"
            #print self._attrib[inf_id]
            print "_attrib[", inf_id, "]", self._attrib[inf_id]
        """
        #TODO: to here

        for attrib in attribOnThisPage:
            normalizedAttrib = normalize(attrib)
            if normalizedAttrib not in self._attrib[inf_id]:
                self._attrib[inf_id].add(normalizedAttrib)
                newAttrib.append(normalizedAttrib)

        #TODO: delete from here
        """
        if page._id == u"316" or page._id == u"324":
            print "_attrib[", inf_id, "]", self._attrib[inf_id]
            print "print new attr:", newAttrib
        """
        #TODO: to here

        return inf_id, newAttrib


    def getAttrib(self, page):
        if page._id not in self._infPageDescription:
            return None

        #get bodies of all templates that looks like infobox
        templates = infoboxBodyGetter.findall(page._text)

        for t in templates: #for every template found in text
            infoboxName = normalize(t[0])
            for infoboxDiscr in self._infPageDescription[page._id]: #for every infobox associated with this page
                if infoboxDiscr[0] == infoboxName: #if template name is infobox
                    attribList = (infoboxDiscr[1], [])
                    parsedAttrib = None

                    try:
                        parsedAttrib = atributeSet.parseString(t[1])
                    except ParseException, e:
                        print "ParseException", e.msg
                        return None

                    for attrib in parsedAttrib:
                        attribList[1].append(attrib[0])
                    return attribList

        print "for page with id " + page._id.encode("utf-8") + " there are some infoboxes but can`t match anyone"
        #print self._infPageDescription[page._id]

        #TODO: delete from here
        #for t in templates:
        #    print normalize(t[0])
        #exit(1)
        #TODO: to here


        return None


